// pages/oa/logs/index.js
var api = require("../../../utils/api.js");
var orgid = '';
var userId = '';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    TypeItem: 0,
    navbar: ['全部', '未读', '我发出的'],
    currentTab: 0,
    detail: true,
    write: false,
    pageSize: 10,

    pageNum: 1,//我发出的
    sendOutData: [],//我发出的日志
    hasMoreData: true,

    pageNum1: 1,//未读的
    unreadData: [],//未读的日志
    hasMoreData1: true,

    pageNum2: 1,//全部的
    allData: [],//全部的日志
    hasMoreData2: true,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: "工作日志"
    });

    orgid = wx.getStorageSync('companyId');
    userId = wx.getStorageSync('userId');
    //获取我发出的日志
    this.getSendOutData("正在加载数据...");
    //获取未读的日志
    this.getUnreadData("正在加载数据...");
    //获取全部的日志
    this.getAllData("正在加载数据...");

  },
  /**
   * 获取全部
   */
  getAllData: function (message) {
    var that = this;
    wx.showLoading({
      title: message,
    })
    api.fetchApi2("logManage/queryAllLogsByUserIdForMobile?userId=" + userId + "&pageNum=" + that.data.pageNum2 + "&pageSize=" + that.data.pageSize, {
    }, "POST").then(res => {
      wx.hideLoading();
      var allData = that.data.allData;
      var datas = res.data.data;
      var pageNums = parseInt(res.data.count) / parseInt(that.data.pageSize);
      if (that.data.pageNum2 == 1) {
        allData = [];
      }
      for (let i = 0; i < datas.length; i++) {
        // if (datas[i].open_invoice_time != "" && datas[i].open_invoice_time != "undefined") {
        //   datas[i].open_invoice_time = util.formatTimeDay(new Date(datas[i].open_invoice_time));
        // }
        allData.push(datas[i]);
      }
      if (that.data.pageNum2 >= pageNums) {
        that.setData({
          allData: allData,
          hasMoreData2: false
        })
      } else {
        that.setData({
          allData: allData,
          hasMoreData2: true,
          pageNum2: that.data.pageNum2 + 1
        })
      }
      // complete
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    });
  },
  /**
   * 获取未读的日志
   */
  getUnreadData: function (message) {
    var that = this;
    wx.showLoading({
      title: message,
    })
    api.fetchApi2("logManage/queryUnreadLogsByUserIdForMobile?userId=" + userId + "&pageNum=" + that.data.pageNum1 + "&pageSize=" + that.data.pageSize, {
    }, "POST").then(res => {
      wx.hideLoading();
      var unreadData = that.data.unreadData;
      var datas = res.data.data;
      var pageNums = parseInt(res.data.count) / parseInt(that.data.pageSize);
      if (that.data.pageNum1 == 1) {
        unreadData = []
      }
      for (let i = 0; i < datas.length; i++) {
        // if (datas[i].open_invoice_time != "" && datas[i].open_invoice_time != "undefined") {
        //   datas[i].open_invoice_time = util.formatTimeDay(new Date(datas[i].open_invoice_time));
        // }
        unreadData.push(datas[i]);
      }
      if (that.data.pageNum1 >= pageNums) {
        that.setData({
          unreadData: unreadData,
          hasMoreData1: false
        })
      } else {
        that.setData({
          unreadData: unreadData,
          hasMoreData1: true,
          pageNum1: that.data.pageNum1 + 1
        })
      }
      // complete
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    });
  },

  /**
   * 获取我发出当然日志信息
   */
  getSendOutData: function (message) {
    var that = this;
    wx.showLoading({
      title: message,
    })
    api.fetchApi2("logManage/queryLogsByUserIdForMobile?userId=" + userId + "&pageNum=" + that.data.pageNum + "&pageSize=" + that.data.pageSize, {
    }, "POST").then(res => {
      wx.hideLoading();
      var sendOutData = that.data.sendOutData;
      var datas = res.data.data;
      var pageNums = parseInt(res.data.count) / parseInt(that.data.pageSize);
      if (that.data.pageNum == 1) {
        sendOutData = []
      }
      for (let i = 0; i < datas.length; i++) {
        // if (datas[i].open_invoice_time != "" && datas[i].open_invoice_time != "undefined") {
        //   datas[i].open_invoice_time = util.formatTimeDay(new Date(datas[i].open_invoice_time));
        // }
        sendOutData.push(datas[i]);
      }
      if (that.data.pageNum >= pageNums) {
        that.setData({
          sendOutData: sendOutData,
          hasMoreData: false
        })
      } else {
        that.setData({
          sendOutData: sendOutData,
          hasMoreData: true,
          pageNum: that.data.pageNum + 1
        })
      }
      // complete
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      pageNum: 1,
      pageNum1: 1,
      pageNum2: 1
    }, () => {
      //获取我发出的日志
      this.getSendOutData("正在加载数据...");
      //获取未读的日志
      this.getUnreadData("正在加载数据...");
      //获取全部的日志
      this.getAllData("正在加载数据...");
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    //我发出的
    if (that.data.currentTab == 2) {
      if (that.data.hasMoreData) {
        that.getSendOutData("加载更多数据");
      } else {
        wx.showToast({
          title: '没有更多数据',
        })
      }
    }
    //未读的
    if (that.data.currentTab == 1) {
      if (that.data.hasMoreData1) {
        that.getUnreadData("加载更多数据");
      } else {
        wx.showToast({
          title: '没有更多数据',
        })
      }
    }
    //全部的
    if (that.data.currentTab == 0) {
      if (that.data.hasMoreData2) {
        that.getAllData("加载更多数据");
      } else {
        wx.showToast({
          title: '没有更多数据',
        })
      }
    }

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  redirectTo: function (e) {
    var that = this;
    var idx = e.currentTarget.id;
    if (idx == 0) {
      wx.navigateTo({
        url: 'write'
      });
    } else {
      wx.navigateTo({
        url: 'index'
      });
    }
  },
  //响应点击导航栏
  navbarTap: function (e) {
    var that = this;
    that.setData({
      currentTab: e.currentTarget.dataset.idx,
      TypeItem: that.data.navbar[that.data.currentTab]
    })
  },
  showdetail: function (option) {
    var id = option.currentTarget.dataset.id;
    var flag = option.currentTarget.dataset.flag;
    var currentTab = this.data.currentTab;
    //全部
    // if (currentTab==0){
    //   if (flag == 1){
    //     //我发出的
    //     wx.navigateTo({
    //       url: "write?id=" + id + "&flag=" + flag + "&currentTab=" + currentTab   //通知详情
    //     });
    //   }
    //   if (flag == 2){
    //     wx.navigateTo({
    //       url: "detail?id=" + id + "&flag=" + flag + "&currentTab=" + currentTab   //通知详情
    //     });
    //   }
    // }
    // //未读
    // if (currentTab == 1) {
    //   wx.navigateTo({
    //     url: "detail?id=" + id + "&flag=" + flag + "&currentTab=" + currentTab   //通知详情
    //   });
    // }
    // //我发出的
    // if (currentTab == 2) {
    //   wx.navigateTo({
    //     url: "write?id=" + id + "&flag=" + flag + "&currentTab=" + currentTab   //通知详情
    //   });

    // }

    wx.navigateTo({
      url: "detail?id=" + id + "&flag=" + flag + "&currentTab=" + currentTab   //通知详情
    });
  },
})